<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PricelistController
 *
 * @author admin
 */
namespace Home\Controller;
use Think\Controller;
class PricelistController extends HomeController{
    //put your code here
    public function pricelist(){
        if(isset($_GET['label'])){
            $goodsres=$this->get_data($_GET['label'] );
            $title=$_GET['chinese'];
        }else{ 
            $goodsres=$this->get_data("");
            $title="全部商品";
        }
            $labelTable=D('config');
            $where['name']='LABEL_SET';

            $labellist=$labelTable->where($where)->getfield('value');
            $str= explode("\n", $labellist);
            $arr=array();
            foreach ($str as $value){
               $arr2=explode(":", $value);
                       if($arr2[0]==""||$arr2[0]==" "||$arr2[0]==null){
                       }else{
                       $arr[$arr2[0]]=$arr2[1];
                       }
            }
                    $this->title=$title;
                    $this->pricelist=$goodsres;
                    $this->label=$arr;
                    $this->display();
    }
    function get_data($keyword){
        $tablegoods=D('product');
        if($keyword==""||$keyword==null||$keyword==" "){
            $sql="select * from gr_product where status=1 order by recommend desc,inventory desc,createdate desc ";
        }else{  $sql="select * from gr_product where status=1 and tagarray like '%".$keyword."%' order by recommend desc,inventory desc,createdate desc ";}
          $goodsres=$tablegoods->query($sql);
         $i=0;  
        foreach($goodsres as $value){
           preg_match_all('/\["(.+?)\"(.+?)/', $value["picurlarray"], $arr4);
             $goodsres[$i]['imgurl']=$arr4[1][0];
             $data['pid']=$goodsres[$i]['id'];
             $data['uid']=session('userid');
             $flagres=D('cart')->where($data)->select();
                if(count($flagres)==0){
                    $iscartflag=false;
                }else{
                    $iscartflag=true;
                }
             $goodsres[$i]['iscart']=$iscartflag;
             $i++; 
        }
         return $goodsres;
       }
    public function addToCart(){
        $uid = session('userid');
        $pid = $_POST['pid'];
        $data['uid']=$uid;
        $data['pid']=$pid;
        M('cart')->where($data)->delete();
        $data['pnum']=1;
        $data['status']=1;
        $result= M('cart')->add($data);        
        if($result){
            $this->ajaxReturn($result);
        }
    }
    public function delFromCart(){
        $uid = session('userid');
        $pid = $_POST['pid']; 
        //$map=array('uid'=>$uid,'pid'=>$pid);
        $map['uid'] = $uid;
        $map['pid'] = $pid;
        $result= M('cart')->where($map)->delete();     
        if($result){
            $this->ajaxReturn($result);
        }
    }
}

